package com.example.swd1.gudong.main.sport.normalsport;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.Tool.DatabaseHelper;
import com.example.swd1.gudong.constant.Constant;
import com.example.swd1.gudong.main.user.historyrecord.HistoryRecordActivity;

import java.text.DecimalFormat;


//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    NormalSportFragment
//   Reference documents : xx
//==================================================================================================================
public class NormalSportFragment extends Fragment implements View.OnClickListener {
    private TextView allDistance, allHours, allTimes;
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase readableDatabase;
    private float allDistanceValue = 0, allHoursValue = 0;
    private int allTimesValue = 0;
    private DecimalFormat formatForDistance;
    private DecimalFormat formatForHours;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        databaseHelper = DatabaseHelper.getInstance(getContext());

        /*-begin-201609012-shile.wu-add-for-task2781345-*/
        //old: databaseHelper = new DatabaseHelper(getContext());
        databaseHelper = DatabaseHelper.getInstance(getContext());
        /*-begin-201609012-shile.wu-add-for-task2781345-*/

        readableDatabase = databaseHelper.getReadableDatabase();
        formatForDistance = new DecimalFormat("0.00");
        formatForHours = new DecimalFormat("0.0");
        queryDataFromDB();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sport_normal_page, container, false);
        allDistance = (TextView) view.findViewById(R.id.tv_distiance_all);
        allHours = (TextView) view.findViewById(R.id.tv_hours_all);
        allTimes = (TextView) view.findViewById(R.id.tv_times_all);
        Typeface typeFace = Typeface.createFromAsset(getActivity().getAssets(), "fonts/din_condensed_bold.ttf");

        allDistance.setTypeface(typeFace);
        allHours.setTypeface(typeFace);
        allTimes.setTypeface(typeFace);

        allDistance.setText(formatForDistance.format(allDistanceValue / 1000) + "");
        allHours.setText(formatForHours.format(allHoursValue / 3600) + "");
        allTimes.setText(allTimesValue + "");

        allDistance.setOnClickListener(this);
        return view;
    }

    public void queryDataFromDB() {
        Cursor cursor = readableDatabase.query(Constant.DATABASE_TABLE_NAME_SPORT_RECORD, null, null, null, null, null, null);
        if (cursor.getCount() != allTimesValue) {
            while (cursor.moveToNext()) {
                allDistanceValue += cursor.getFloat(cursor.getColumnIndex(Constant.DATABASE_TABLE_FIELD_DISTANCE));
                allHoursValue += cursor.getFloat(cursor.getColumnIndex(Constant.DATABASE_TABLE_FIELD_HOURS));
                allTimesValue++;
            }
        }
        cursor.close();
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getActivity(), HistoryRecordActivity.class));
    }
}
